<style>
</style>
<template>
	<div class="notice-box el-dialog__wrapper">
        <div class="v-modal" @click="handleClose"></div>
        <div class="el-dialog ">
            <div class="el-dialog__header">
                <span class="el-dialog__title">公告</span>
                <button type="button" aria-label="Close" class="el-dialog__headerbtn" @click="handleClose">
                    <i class="el-dialog__close el-icon el-icon-close"></i>
                </button>
            </div>
            <div class="el-dialog__body">
                <div class="notice-tab">
                    <el-button type="text" :class="{'curr':!isAll}" @click="handleTab(false)">未知悉</el-button>
                    <el-button type="text" :class="{'curr':isAll}" @click="handleTab(true)">全部</el-button>
                </div>
                <div class="notice-main" v-if="filteredLis.length > 0">

                    <div class="lis-main" v-for="(item,index) in filteredLis" :key="index">
                        <time class="time">{{item.day}}（{{item.lis.length}}）</time>
                        <dl v-for="(noticei,nindex) in item.lis" class="item" :key="nindex">
                            <dt class="title">
                            <div class="text-l">{{noticei.title}} </div>
                            <span class="item-badge" :style="'background-color:'+noticei.badge.color">{{noticei.badge.text}}</span>
                            </dt>
                            <dd>
                                <p class="text">{{noticei.text}}</p>
                                <div class="handle">
                                    <el-button  size="mini" :class="{'is-know':noticei.isknowk}" @click="handleKnow(noticei)">已知悉</el-button>
                                </div>
                                <p class="des">
                                    {{noticei.creator}}
                                    <time class="time">{{noticei.createdtime.substr(noticei.createdtime.length-8,noticei.createdtime.length-3)}}</time>
                                </p>

                            </dd>
                        </dl>
                    </div>

                </div>
                <div class="empty" v-else>暂无数据</div>
            </div>

        </div>
	</div>
</template>

<script>
    import {noticeLisUrl,isKnowUrl} from '../common/ajax'
	export default{
        name:'NoticeNode',
        props:{
            ks:{
                type:Object,
                default:{ld:'' ,lc:'' ,ks:''}
            }
        },
		data(){
			return {
				lis:[],
                isAll:false,
                isHandle:true
			}
		},
		components: {},
        created(){
//            this.handleGetLis()
        },
        computed: {
            filteredLis: function () {
                var search = this.isAll;
                if (!search) {
                    let lis = [];
                    for(var val of this.lis){
                        let o = {day:val.day,lis:[]}; //
                        for(var n of val.lis){
                            if(!n.isknowk){
                                o.lis.push(n)
                            }
                        }
                        if(o.lis.length > 0){
                            lis.push(o)
                        }
                    }
                    return lis
                }
                return this.lis;
            }
        },
        methods:{
            handleGetLis(ks){
                jQuery.ajax({
                    url: noticeLisUrl,
                    type: 'get',
                    dataType: 'json',
                    cache: false,
                    data:ks,
                    success: function (data) {
                        if (data.success) {
                            let lis = data.data.sort(function(a,b){
                                return new Date(b.day).getTime() - new Date(a.day).getTime()
                            });
                            let noklis = [],newlis = [];
                            for(var val of lis){
                                val.lis.sort(function(a,b){
                                    return b.id - a.id
                                });
                                let noko = {day:val.day,lis:[]}, allo = {day:val.day,lis:[]}; //
                                for(var n of val.lis){
                                    n['isknowk'] = false;
                                    //比较是否为这个科室的
                                    if(n.ks.length == 0 || this.handleIsExists(n.ks,ks)){
                                        //没有特定的
                                        //是否查看了
                                        if(n.isknowks.length > 0){
                                            //有已知悉的
                                            if(this.handleIsExists(n.isknowks,ks)){
                                                n.isknowk = true
                                            }else {
                                                noko.lis.push(n)
                                            }
                                        }else {
                                            //没有已知悉的
                                            noko.lis.push(n)
                                        }
                                        allo.lis.push(n);
                                    }
                                }
                                if(noko.lis.length > 0){
                                    noklis.push(noko)
                                }
                                if(allo.lis.length > 0){
                                    newlis.push(allo)
                                }
                            }
                            this.lis = newlis
                            if(noklis.length > 0){
                                this.$emit('change',true)
                            }else {
                                this.$emit('change',false)
                            }
                        }else {
                            throwErrorTips('获取公告列表失败',data.msg)
                        }
                    }.bind(this),
                    error:function(xhr,status){
                        throwErrorTips('获取公告列表失败',status)
                    }.bind(this)
                });
            },
            handleTab(s){
                this.isAll = s;
            },
            handleIsExists(arr,ks){
                let ret = false,
                    i = -1,
                    ksl = ks.ks.split(' / '),
                    kslen = ksl.length;
                arr.forEach((value,index)=>{
                    let vks = value.ks.split(' / ');
                    if(value.ld == ks.ld && value.lc == ks.lc  &&  vks.length == kslen){
                        let n = 0;
                        vks.forEach(function(rval,rind){
                            if(ksl.indexOf(jQuery.trim(rval)) != -1){
                                n++
                            }
                        });
                        if(n == kslen){
                            i = index;
                            return
                        }

                    }
                })
                if(i != -1){
                    ret = true
                }
                return ret
            },
            handleKnow(val){
                if(!val.isknowk && this.isHandle){
                    this.isHandle = false;
                    jQuery.ajax({
                        url: isKnowUrl,
                        type: 'get',
                        dataType: 'json',
                        cache: false,
                        data:{id:val.id,ks:this.ks},
                        success: function (data) {
                            this.isHandle = true;
                            if (data.success) {
                                this.handleGetLis(this.ks)
                            }else {
                                throwErrorTips('已知悉失败',data.msg)
                            }
                        }.bind(this),
                        error:function(xhr,status){
                            this.isHandle = true;
                            throwErrorTips('已知悉失败',status)
                        }.bind(this)
                    });
                }

            },
            handleClose(){
                this.$emit('close')
            }
        }
	}
</script>
